package com.moxiao.yfjh.module.blind.service.memberLike;

import com.moxiao.yfjh.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.moxiao.yfjh.module.blind.controller.app.controller.exchage.vo.AppMemberLikeReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.module.blind.controller.admin.memberLike.vo.MemberLikeCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberLike.vo.MemberLikeExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberLike.vo.MemberLikePageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberLike.vo.MemberLikeUpdateReqVO;
import com.moxiao.yfjh.module.blind.dal.dataobject.memberSayHello.MemberSayhelloDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.memberLike.vo.*;
import com.moxiao.yfjh.module.blind.dal.dataobject.memberLike.MemberLikeDO;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.blind.convert.memberLike.MemberLikeConvert;
import com.moxiao.yfjh.module.blind.dal.mysql.memberLike.MemberLikeMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲会员喜欢我 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MemberLikeServiceImpl implements MemberLikeService {

    @Resource
    private MemberLikeMapper memberLikeMapper;

    @Override
    public Long createMemberLike(MemberLikeCreateReqVO createReqVO) {
        // 插入
        MemberLikeDO memberLike = MemberLikeConvert.INSTANCE.convert(createReqVO);
        memberLikeMapper.insert(memberLike);
        // 返回
        return memberLike.getId();
    }

    @Override
    public void updateMemberLike(MemberLikeUpdateReqVO updateReqVO) {
        // 校验存在
        validateMemberLikeExists(updateReqVO.getId());
        // 更新
        MemberLikeDO updateObj = MemberLikeConvert.INSTANCE.convert(updateReqVO);
        memberLikeMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberLike(Long id) {
        // 校验存在
        validateMemberLikeExists(id);
        // 删除
        memberLikeMapper.deleteById(id);
    }

    private void validateMemberLikeExists(Long id) {
        if (memberLikeMapper.selectById(id) == null) {
            throw exception(MEMBER_LIKE_NOT_EXISTS);
        }
    }

    @Override
    public MemberLikeDO getMemberLike(Long id) {
        return memberLikeMapper.selectById(id);
    }

    @Override
    public List<MemberLikeDO> getMemberLikeList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberLikeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MemberLikeDO> getMemberLikePage(MemberLikePageReqVO pageReqVO) {
        return memberLikeMapper.selectPage(pageReqVO);
    }

    @Override
    public Long getMemberLikeCount(MemberLikePageReqVO pageReqVO) {
        return memberLikeMapper.selectCount(pageReqVO);
    }

    @Override
    public List<MemberLikeDO> getMemberLikeList(MemberLikeExportReqVO exportReqVO) {
        return memberLikeMapper.selectList(exportReqVO);
    }

    @Override
    public Long getMemberLikeByUserId(Date todayStart, Date todayEnd, String userId) {

        LambdaQueryWrapper<MemberLikeDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(MemberLikeDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.eq(MemberLikeDO::getIssue,userId);
        return memberLikeMapper.selectCount(lqw);
    }

    @Override
    public List<MemberLikeDO> getMemberLikeList(AppMemberLikeReqVO appMemberLikeReqVO) {
        return memberLikeMapper.selectLikeList(appMemberLikeReqVO);
    }

    @Override
    public MemberLikeDO getBlindMemberLike(AppMemberLikeReqVO appMemberLikeReqVO) {
        LambdaQueryWrapperX<MemberLikeDO> memberSayHelloDOWrapperX = new LambdaQueryWrapperX<>();
        memberSayHelloDOWrapperX.and(wrapper->wrapper.eq(MemberLikeDO::getReceive, appMemberLikeReqVO.getReceive()).eq(MemberLikeDO::getIssue, appMemberLikeReqVO.getIssue()))
                .or(wrapper->wrapper.eq(MemberLikeDO::getReceive, appMemberLikeReqVO.getIssue()).eq(MemberLikeDO::getIssue, appMemberLikeReqVO.getReceive()));
        return memberLikeMapper.selectOne(memberSayHelloDOWrapperX);
    }

    @Override
    public Long likeMeNum(Long userId) {
        LambdaQueryWrapper<MemberLikeDO> lqw = Wrappers.lambdaQuery();
        lqw.and(wrapper->wrapper.eq(MemberLikeDO::getReceive, userId))
                .or(wrapper->wrapper.eq(MemberLikeDO::getIssue, userId).eq(MemberLikeDO::getLikeEachother, (byte) 1));
        return memberLikeMapper.selectCount(lqw);
    }

}
